package db

import (
	"fmt"
	"log"
	"time"

	"gitee.com/diandisoft/ant_forest_energy_web_server/config"
	"gopkg.in/mgo.v2"
)

var (
	// Session stores mongo session
	Session *mgo.Session

	// Mongo stores the mongodb connection string information
	Mongo *mgo.DialInfo
)

// Connect connects to mongodb
func Connect() {
	config := config.GetConfig()
	server := config.GetString("db.server")
	database := config.GetString("db.database")
	log.Printf("数据库连接成功！地址：%s，库：%s\n", server, database)

	Mongo = &mgo.DialInfo{
		Addrs: []string{server},
		//		Username: AppConfig.DBUser,
		//		Password: AppConfig.DBPwd,
		Timeout:  10 * time.Second,
		Database: database,
	}

	s, err := mgo.DialWithInfo(Mongo)
	if err != nil {
		fmt.Printf("无法连接数据库，error %v\n", err)
		panic(err.Error())
	}
	s.SetSafe(&mgo.Safe{})
	fmt.Println("数据库连接成功!")
	Session = s

}
